using System;
using System.Text;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    //Table-Mapping Class (Customisable half)
    public partial class CTagType
    {
        #region Constants
        #endregion

        #region Constructors (Public)
        //Default Connection String
        public CTagType() : base() {}

        //Alternative Connection String
        public CTagType(CDataSrc dataSrc) : base(dataSrc) {}
        
        //Hidden  (UI code should use cache instead)
        protected internal CTagType(int typeId) : base(typeId) {}
        protected internal CTagType(CDataSrc dataSrc, int typeId) : base(dataSrc, typeId) {}
        protected internal CTagType(CDataSrc dataSrc, int typeId, IDbTransaction txOrNull) : base(dataSrc, typeId, txOrNull) { }
        #endregion

        #region Default Values
        protected override void InitValues_Custom()
        {
            //_sampleDateCreated = DateTime.Now;
            //_sampleSortOrder   = 0;
        }
		#endregion

		#region Default Connection String
		protected override CDataSrc DefaultDataSrc() { return CConfig.Photos; } //CDataSrc.Default;  }
		#endregion

		#region Properties - Relationships
		//Relationships - Foriegn Keys (e.g parent)
		public CApp App { get { return CApp.Cache.GetById(this.TypeAppId); } }


		//Relationships - Collections (e.g. children)
        public    CMetaTagList MetaTags                     {  get { return CMetaTag.Cache.GetByTypeId(          this.TypeId); }  }
        protected CMetaTagList MetaTags_(IDbTransaction tx) {        return new CMetaTag(DataSrc).SelectByTypeId(this.TypeId, tx); } //Only used for cascade deletes

		#endregion

		#region Properties - Customisation
		//Derived/ReadOnly (e.g. xml classes, presentation logic)

		public string NameAndCount
		{
			get
			{
				return CUtilities.NameAndCount(TypeName, MetaTags);
			}
		}
		public string FullName
		{
			get
			{
				return string.Concat(App.AppName, " :: ", NameAndCount);
			}
		}
		public string FullName_
		{
			get
			{
				return string.Concat(FullName, this.TagIsOneToOne ? " *one" : "*multi" );
			}
		}
		#endregion

		#region Save/Delete Overrides
		//Can Override base.Save/Delete (e.g. Cascade deletes, or insert related records)
		#endregion

		#region Custom Database Queries
		//(Not normally required for cached classes, use list class for searching etc)
		//For Stored Procs can use: MakeList (matching schema), or DataSrc.ExecuteDataset (reports etc)
		//For Dynamic sql, can use: SelectSum, SelectDistinct, SelectCount, SelectWhere (inherited methods)
		#endregion

		#region Searching (Optional)
		//For cached classes, custom seach logic resides in static methods on the list class
		// e.g. CTagType.Cache.Search("...")

		//See also the auto-generated methods based on indexes
		//' e.g. CTagType.Cache.GetBy...
		#endregion

		#region Caching Details
		//Cache Key
		internal static string CACHE_KEY = typeof(CTagType).ToString();    //TABLE_NAME

        //Cache data
        private static CTagTypeList LoadCache()  {   return new CTagType().SelectAll();   }
        //Cache Timeout
        private static void SetCache(CTagTypeList value)
        {
            if (null != value)  
                value.Sort(); 
            CCache.Set(CACHE_KEY, value);    //Optional parameter can override timeout (otherwise uses config-settings, which default to 3hrs)
        }
        //Helper Method
        private CTagType CacheGetById(CTagTypeList list)  { return list.GetById(this.TypeId);    }
        #endregion

        #region Cloning
        public CTagType Clone(CDataSrc target, IDbTransaction txOrNull) //, int parentId)
        {
            //Shallow copy: Copies the immediate record, excluding autogenerated Pks
            CTagType copy = new CTagType(this, target);

            //Deep Copy - Child Entities: Cloned children must reference their cloned parent
            //copy.SampleParentId = parentId;

            copy.Save(txOrNull);

            //Deep Copy - Parent Entities: Cloned parents also clone their child collections
            //this.Children.Clone(target, txOrNull, copy.TypeId);

            return copy;
        }
        #endregion

        #region ToXml
        protected override void ToXml_Custom(System.Xml.XmlWriter w)
        {
            //Store(w, "Example", this.Example)
        }
        #endregion
    }
}